//=============================================================================
// Shield.
//=============================================================================

class TFShield extends TFStation
	placeable;

var tfBlockShield shield;
var int healcount;
var int healradius;

replication
{
	// Things the server should send to the client.
	reliable if ( Role == ROLE_Authority )
		Shield, healcount;
}

simulated function PreBeginPlay()
{
    super.PreBeginPlay();

    if ( Role < ROLE_Authority ) return;

    shield=spawn(class'Taskforces.tfBlockShield');
    if(shield!=none) shield.shield=self;
}

function Upgrade(pawn p)
{
    super.Upgrade(p);
    if(grade==2)
    {
        Health=Health/MaxHealth*1000;
        MaxHealth=1000;
    }
    else if(grade==3)
    {
        Health=Health/MaxHealth*2000;
        MaxHealth=2000;
    }
}

function TakeDamage(int Damage, Pawn instigatedBy, Vector hitlocation, Vector momentum, class<DamageType> damageType)
{
	local float tmp, factor;

	if ( Role < ROLE_Authority ) return;

    factor=1;

	if((vector(Rotation) dot (hitlocation-Location))>0.2 && grade>0)
        factor/=grade+1;

	tmp=damage*factor;
    damage*=factor;
    tmp-=damage;
    if(tmp<1 && frand()<tmp) tmp=1;
       damage+=tmp;

	super.takedamage(damage,instigatedBy,hitlocation,momentum,damageType);
}

state Ready
{
    function BeginState()
    {
        super.BeginState();

        if(myteam==0) setstaticmesh(StaticMesh'TFMedia.Buildings.tfShieldRed');
        else setstaticmesh(StaticMesh'TFMedia.Buildings.tfShieldBlu');

        settimer(0.2,true);
    }

    function Timer()
    {
        local tfstation s;
        local sentinel sent;
        local sentinelplayerreplicationinfo spri;

        if(grade==2 && healcount>=10)
        {
            spawn(class'Taskforces.tfshieldhealfx');
            healcount=0;

            foreach VisibleCollidingActors(class'tfstation',s,2*healradius,location)
            {
			    if(s==self && s.myteam==myteam)
				{
				s.Health+=fmax(0,fmin(5*((healradius-vsize(location-s.location))/healradius), s.MaxHealth-s.Health));
				}
                else if(tfbasecore(s)==none && s!=self && s.Grade>0 && s.MyTeam==myteam)
                {
                    s.Health+=fmax(0,fmin(20*((healradius-vsize(location-s.location))/healradius), s.MaxHealth-s.Health));
                }
            }
            foreach VisibleCollidingActors(class'sentinel',sent,2*healradius,location)
            {
                if(sent!=none && spri!=none && spri.Team.TeamIndex==myteam)
                {
                    sent.Health+=fmax(0,fmin(20*((healradius-vsize(location-sent.location))/healradius), sent.HealthMax-sent.Health));
                }
            }
        }
        else if(grade==3) FindNear();

        healcount++;
    }
}

defaultproperties
{
     MaxHealth=2500.000000
     BuildingName="Shield"
     NameIndex=1
     directional=True
     teamflareDS=1.200000
     StaticMesh=StaticMesh'TFMedia.Buildings.tfShield'
     DrawScale=1.600000
     healradius=360
}
